﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;

namespace StoragePeriod.Common
{
    public static class ControlExtensions
    {
        public static IEnumerable<Control> All(this ControlCollection controls)
        {
            foreach (Control control in controls)
            {
                foreach (var grandchild in control.Controls.All())
                    yield return grandchild;

                yield return control;
            }
        }

        public static IEnumerable<T> AllOfType<T>(this ControlCollection controls) where T : Control
        {
            foreach (Control control in controls)
            {
                foreach (var grandchild in control.Controls.AllOfType<T>())
                    yield return grandchild;

                if (control is T)
                    yield return control as T;
            }
        }
         
        public static IEnumerable<T> AllOfTypeNoRecursion<T>(this ControlCollection controls) where T : Control
        {
            foreach (Control control in controls)
            {
                if (control is T)
                    yield return control as T;

                foreach (var grandchild in control.Controls.AllOfTypeNoRecursion<T>())
                    yield return grandchild;
            }
        }
    }
}